package com.travel.admin.util.prewarn;


import com.travel.admin.enums.LogLevelEnum;
import com.travel.admin.exception.TravelExceptionCode;
import com.travel.admin.util.LogUtil;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.net.URLEncoder;


/**
 * @Author hongming
 * @Date 2023/09/28 4:52 下午
 * @Version 1.0
 */
@Component
public class ChatBotSend {
    @Value("${ali.ding.web_hook}")
    private String web_hook ;
    @Value("${ali.ding.secret}")
    private String secret;

    public static void main(String[] args) {
        ChatBotSend chatBotSend = new ChatBotSend();
        chatBotSend.sendMsg("gateway method test-warn/ ", TravelExceptionCode.SYSTEM_ERROR);
    }

    /**
     * 发送钉群机器人监控信息
     */
    public  void sendMsg(String msg, TravelExceptionCode zlcpdExceptionCode){
        sign();
        try {
            msg = msg.concat(zlcpdExceptionCode.getDesc());
            String textMsg = "{ \"msgtype\": \"text\", \"text\": {\"content\":" + "\"" + msg  + "\"}}";
            HttpClient httpclient = HttpClients.createDefault();
            HttpPost httppost = new HttpPost(web_hook);
            httppost.addHeader("Content-Type", "application/json; charset=utf-8");
            StringEntity se = new StringEntity(textMsg, "utf-8");
            httppost.setEntity(se);
            HttpResponse response = httpclient.execute(httppost);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                String result = EntityUtils.toString(response.getEntity(), "utf-8");
                LogUtil.addLog(LogLevelEnum.WARN,"sendMsg, result: {}",result);
            }
        } catch (IOException e) {
            LogUtil.addLog(LogLevelEnum.WARN,"sendMsg: {} warn:{}", msg, e);
        }
    }

    /**
     * 加签名信息
     */
    private void sign() {
        try {
            Long timestamp = System.currentTimeMillis();
//            web_hook = "https://oapi.dingtalk.com/robot/send?access_token=2cd6224289594f3375fb7baaa99a3f4849c22e7a4e465dc2573e085000d39e3b";
//            secret = "SEC8b85852d5e1bf28a3c0c07427a2217bd68e3a352c08a8398af3c35313fb2f633";
            String stringToSign = timestamp + "\n" + secret;
            Mac mac = Mac.getInstance("HmacSHA256");
            mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
            byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
            String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)),"UTF-8");
            web_hook = web_hook+"&timestamp="+timestamp+"&sign="+sign;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
